#include "./../inc/toolbar.hpp"

Toolbar::Toolbar(LCD &lcd_ref) : lcd(lcd_ref)
{
}

void Toolbar::showToolbar()
{
    //  获取 LCD 资源
    int *lcd_mmap = lcd.get_lcd_mmap();
    int lcd_width = lcd.get_lcd_width();
    if (lcd_mmap == NULL)
    { // 避免空指针崩溃
        printf("get lcd mmap failed\n");
        return;
    }

    //  显示 BMP 图片
    BMP pen("./res/pen.bmp", 40, 40);
    pen.showBMP(lcd, 5, 5);

    BMP eraser("./res/eraser.bmp", 40, 40);
    eraser.showBMP(lcd, 55, 5);

    BMP reset("./res/reset.bmp", 40, 40);
    reset.showBMP(lcd, 755, 5);

    // 绘制工具栏元素
    for (int row = 0; row < 48; row++)
    {
        for (int col = 0; col < lcd_width; col++)
        {
            if (lcd_mmap[row * lcd_width + col] == (int)WHITE)
            {
                lcd_mmap[row * lcd_width + col] = GRAY;
            }
            // 半径5的圆（x:155-195, y:5-45）
            if (row >= 5 && row < 45 && col >= 155 && col < 195)
            {
                int center_x = 155 + 20;
                int center_y = 5 + 20;
                int dx = col - center_x;
                int dy = row - center_y;
                if (dx * dx + dy * dy <= 5 * 5)
                {
                    lcd_mmap[row * lcd_width + col] = WHITE;
                }
            }
            // 半径10的圆（x:205-245, y:5-45）
            else if (row >= 5 && row < 45 && col >= 205 && col < 245)
            {
                int center_x = 205 + 20;
                int center_y = 5 + 20;
                int dx = col - center_x;
                int dy = row - center_y;
                if (dx * dx + dy * dy <= 10 * 10)
                {
                    lcd_mmap[row * lcd_width + col] = WHITE;
                }
            }
            // 半径15的圆（x:255-295, y:5-45）
            else if (row >= 5 && row < 45 && col >= 255 && col < 295)
            {
                int center_x = 255 + 20;
                int center_y = 5 + 20;
                int dx = col - center_x;
                int dy = row - center_y;
                if (dx * dx + dy * dy <= 15 * 15)
                {
                    lcd_mmap[row * lcd_width + col] = WHITE;
                }
            }
            // 半径20的圆（x:305-345, y:5-45）
            else if (row >= 5 && row < 45 && col >= 305 && col < 345)
            {
                int center_x = 305 + 20;
                int center_y = 5 + 20;
                int dx = col - center_x;
                int dy = row - center_y;
                if (dx * dx + dy * dy <= 20 * 20)
                {
                    lcd_mmap[row * lcd_width + col] = WHITE;
                }
            }
            // 黄色方块（x:355-405, y:10-40）
            else if (row >= 10 && row < 40 && col >= 355 && col < 405)
            {
                if (col >= 360 && col < 390)
                {
                    lcd_mmap[row * lcd_width + col] = YELLOW;
                }
            }
            // 红色方块（x:405-455, y:10-40）
            else if (row >= 10 && row < 40 && col >= 405 && col < 455)
            {
                if (col >= 410 && col < 440)
                {
                    lcd_mmap[row * lcd_width + col] = RED;
                }
            }
            // 绿色方块（x:455-505, y:10-40）
            else if (row >= 10 && row < 40 && col >= 455 && col < 505)
            {
                if (col >= 460 && col < 490)
                {
                    lcd_mmap[row * lcd_width + col] = GREEN;
                }
            }
            // 蓝色方块（x:505-555, y:10-40）
            else if (row >= 10 && row < 40 && col >= 505 && col < 555)
            {
                if (col >= 510 && col < 540)
                {
                    lcd_mmap[row * lcd_width + col] = BLUE;
                }
            }
        }
    }
}